/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.devs.benchmark.topology;

import java.util.List;
import org.devs.benchmark.Util;
import org.devs.benchmark.modeling.CoupledType;
import org.devs.benchmark.modeling.ModelType;

/**
 *
 * @author Alejandro Moreno
 */
public class Line extends Network {

    int length;
    

    public Line(String name, int length, Class message, int recursion) throws Exception {
        super(name, message, recursion);
        this.length = length;

        if (length<1 || recursion <1) {
            throw new Exception("Parameters are incorrect");
        }


    }



    public Line(String name, int length, Class message) throws Exception {
        super(name, message);
        this.length = length;

        if (length<1) {
            throw new Exception("Parameters are incorrect");
        }
        

    }

    public void buildModel(){

        super.setModel(this.createNetwork(this.name, this.length, super.message, super.recursion));

    }



    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }



    public void connectNodes(CoupledType coupled) {


        if (coupled.getModels().size() > 0) {

            List<ModelType> models = coupled.getModels();

            addConnectionFromParent(coupled, models.size() - 1, 1, 1);
            addConnectionToParent(coupled, models.size() - 1, 0, 0);
            
            addConnectionFromParent(coupled, 0, 0, 0);
            addConnectionToParent(coupled, 0, 1, 1);

            for (int i = 0; i < (models.size() - 1); i++) {

                addConnection(coupled, i, 0, i + 1, 0);
                addConnection(coupled, i + 1, 1, i, 1);
            }
        }

    }

    public ModelType createNetwork(String name, int length, Class message, int recursion) {
        return create(name, length, "", message, recursion);
    }

    public ModelType create(String name, int length, String id, Class message, int recursion) {

        CoupledType coupled = null;

        if (recursion > 0) {

            coupled = new CoupledType(name + "_Coupled" + id, Util.createInports(2, message), Util.createOutports(2, message));
            
            for (int j = 0; j < length; j++) {

                coupled.getModels().add(create(name, length, id + "_" + j, message, recursion - 1));

            }

            connectNodes(coupled);

        } else {


            coupled = buildNode(name, 2, message, id);


        }

        return coupled;

    }


    
}
